package com.szqhyxj.youxiangju.biz.plan.create;

import android.content.Intent;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.BasePresenterImpl;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.yxj.pay.planMaking.PlanFacade;
import com.yxj.pay.planMaking.model.PlanLengthRange;

import java.math.BigDecimal;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

class PlanCreatePresenter extends BasePresenterImpl implements PlanCreateContract.Presenter {

    private PlanCreateContract.View view;
    private PlanModel.PlanCreateEntity planCreateEntity;

    PlanCreatePresenter(PlanCreateContract.View view) {
        this.view = view;
        planCreateEntity = new PlanModel.PlanCreateEntity();
    }

    private int start;

    private int end;

    private int planLength;

    private PlanLengthRange planLengthRange;

    private int billDate;

    private int repayDate;

    public void setBillDate(int billDate) {
        this.billDate = billDate;
    }

    public void setRepayDate(int repayDate) {
        this.repayDate = repayDate;
    }

    @Override
    public void extractIntent(Intent intent) {
        String cardNo = intent.getStringExtra(PlanCreateContract.CARD_NO);
        billDate = intent.getIntExtra(PlanCreateContract.BILL_DATE, 0);
        repayDate = intent.getIntExtra(PlanCreateContract.REPAY_DATE, 0);
        String merchantArea = intent.getStringExtra(PlanCreateContract.MERCHANT_AREA);

        view.showCardNo(cardNo);
        view.showBillDate(billDate);
        view.showRepayDate(repayDate);
        view.showMerchantArea(merchantArea);
    }

    @Override
    public void calculatePlanLength(int progress) {
        int planLength = start + (end - start) * progress / 100;
        this.planLength = planLength;
        view.showPlanLength(planLength);
    }

    @Override
    public void init(int billDate, int repayDate, int ordinary, int exquisite, int highQuality) {

    }

    @Override
    public void createPlan(String amount) {
        if (amount.equals("")) {
            view.showToast("请输入账单金额");
            return;
        }

        startRequest(PlanModel.create(this.planCreateEntity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<PlanModel.PlanCreateResult>() {
                    @Override
                    protected void error(String code, String msg) {
                    }

                    @Override
                    protected void successAccept(PlanModel.PlanCreateResult planCreateResult) {

                        view.preview(
                                planCreateResult.getPlanId(),
                                PlanCreatePresenter.this.planCreateEntity
                        );
                    }
                }));
    }


    @Override
    public void setIdCard(String idCard) {
        planCreateEntity.setCardNo(idCard);
    }

    @Override
    public void setplanLength(int planLength) {
        planCreateEntity.setPlanLength(planLength);
    }

    @Override
    public void setBtName(String btName) {
        planCreateEntity.setBtName(btName);
    }

    @Override
    public void responseChange(Integer amount, boolean selectOne, boolean selectTwo, boolean selectThree) {
        Boolean bordinary;
        Boolean bexquisite;
        Boolean bhighQuality;
        if (amount == null) {
            bordinary = PlanFacade.canMake(billDate, repayDate, 5);
            bexquisite = PlanFacade.canMake(billDate, repayDate, 10);
            bhighQuality = PlanFacade.canMake(billDate, repayDate, 20);
        } else {
            planCreateEntity.setTotalAmount(amount);
            bordinary = PlanFacade.canMake(billDate, repayDate, 5) && PlanFacade.checkWorkingAmount(new BigDecimal(amount), 5);
            bexquisite = PlanFacade.canMake(billDate, repayDate, 10) && PlanFacade.checkWorkingAmount(new BigDecimal(amount), 10);
            bhighQuality = PlanFacade.canMake(billDate, repayDate, 20) && PlanFacade.checkWorkingAmount(new BigDecimal(amount), 20);
        }
        Integer ratio = null;
        if (!selectOne) {
            view.unSelect(0);
        }
        if (!selectTwo) {
            view.unSelect(1);
        }
        if (!selectThree) {
            view.unSelect(2);
        }
        if (bordinary) {
            view.enable(0);
            if (selectOne) {
                view.select(0);
                ratio = 5;
            }
        } else {
            view.disable(0);
        }
        if (bexquisite) {
            view.enable(1);
            if (selectTwo) {
                view.select(1);
                ratio = 10;
            }
        } else {
            view.disable(1);
        }
        if (bhighQuality) {
            view.enable(2);
            if (selectThree) {
                view.select(2);
                ratio = 20;
            }
        } else {
            view.disable(2);
        }

        if (!selectOne && !selectTwo && !selectThree) {
            ratio = showDefaultSelected(bordinary, bexquisite, bhighQuality);
        }
        planCreateEntity.setRatio(ratio);
        if (ratio == null) {
            view.disableCreateButton();
            return;
        }
        view.enableCreateButton();
        this.planLengthRange = PlanFacade.getPlanLengthRange(billDate, repayDate, ratio);
        if (planLengthRange == null) {
            return;
        }
        start = planLengthRange.getMinPlanDay();
        end = planLengthRange.getMaxPlanDay();
        view.showPlanLengthRange(planLengthRange.getMinPlanDay(), planLengthRange.getMaxPlanDay());
        view.showPlanLength(planLengthRange.getMinPlanDay());
    }

    private Integer showDefaultSelected(Boolean bordinary, Boolean bexquisite, Boolean bhighQuality) {
        if (bordinary) {
            view.select(0);
            return 5;
        }
        if (bexquisite) {
            view.select(1);
            return 10;
        }
        if (bhighQuality) {
            view.select(2);
            return 20;
        }
        return null;
    }
}